package com.dy.course.controller;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dy.course.common.BaseController;
import com.dy.course.common.ResponseMessage;
import com.dy.course.entity.CourseInfo;
import com.dy.course.entity.CourseSignInfo;
import com.dy.course.enumerate.CourseType;
import com.dy.course.service.ICycleCourseService;
import com.github.pagehelper.PageInfo;

@Controller
public class CycleCourseController extends BaseController {

	@Autowired
	private ICycleCourseService courseService;

	/**
	 * 
	 * 查询周期性课程
	 * 
	 * @param query
	 *            周期性课程查询信息
	 * @return 周期性课程列表
	 */
	@RequestMapping(value = "/course/select")
	@ResponseBody
	public ResponseMessage<PageInfo<CourseInfo>> queryCourse(CourseInfo query) {
		logger.info("【查询周期性课程接口】,参数:query=" + JSON.toJSONString(query));
		List<CourseInfo> pageList = null;
		pageList = courseService.list(query);
		PageInfo<CourseInfo> page = new PageInfo<CourseInfo>(pageList);
		return constructResponse(page);
	}

	/**
	 * 
	 * 查询成员课程
	 * 
	 * @param
	 * @return
	 */
	@RequestMapping(value = "/course/member")
	@ResponseBody
	public ResponseMessage<List<CourseSignInfo>> queryCourseMember(CourseSignInfo cb) {
		logger.info("【查询课程成员接口】,参数:cb=" + JSON.toJSONString(cb));
		List<CourseSignInfo> list = null;
		list = courseService.selectCycleCourseMembers(cb);
		return constructResponse(list);
	}

	/**
	 * 
	 * 创建周期性课程
	 * 
	 * @param course
	 *            周期性课程信息
	 * @return
	 */
	@RequestMapping(value = "/course/create/cycle", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<?> createCycleCourse(@RequestBody CourseInfo course) {
		logger.info("【创建周期性课程接口】,参数:course=" + JSON.toJSONString(course));
		course.setType(CourseType.CYCLE.getCode().byteValue());
		courseService.createCourse(course);
		return constructNullResponse();
	}

	/**
	 * 周期性课程签到
	 * 
	 * @param query
	 * @return
	 */
	@RequestMapping(value = "/course/sign", method = RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<?> sign(@RequestBody CourseSignInfo csi) {
		logger.info("【周期性课程签到接口】参数信息,msi = " + JSON.toJSONString(csi));
		courseService.sign(csi);
		return constructNullResponse();
	}
	
	
	
	/**
	 * 
	 * 创建课程
	 * 
	 * @param course 课程信息
	 * @return 
	 */
	@RequestMapping(value="/course/create/once",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<?> createCourse(@RequestBody CourseInfo course){
		logger.info("【创建课程接口】,参数:course=" + JSON.toJSONString(course));
		course.setType(CourseType.TEMPORARY.getCode().byteValue());
		courseService.createCourse(course);
		return constructNullResponse();
	}
	
	
	/**
	 * 
	 * 删除课堂
	 * 
	 * @param course 删除课堂
	 * @return 
	 */
	@RequestMapping(value="/course/delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<?> delteCourse(@RequestBody CourseInfo course){
		logger.info("【删除课堂接口】,参数:course=" + JSON.toJSONString(course));
		if(course.getCourseType().byteValue() == CourseType.TEMPORARY.getCode())
		{
			courseService.deleteCourseTemplate(course);
		}else{
			if(course.getId() != null){
				courseService.deleteCourseTemplate(course);
			}else{
				courseService.deleteOne(course);	
			}
		}
		return constructNullResponse();
	}
	
	

	/**
	 * 
	 * 删除课堂模板
	 * 
	 * @param course 删除课堂模板
	 * @return 
	 */
	@RequestMapping(value="/course/template/delete",method=RequestMethod.POST)
	@ResponseBody
	public ResponseMessage<?> delteCourseTemplate(@RequestBody CourseInfo course){
		logger.info("【删除课堂模板接口】,参数:course=" + JSON.toJSONString(course));
		courseService.deleteCourseTemplate(course);
		return constructNullResponse();
	}
	
	/**
	 * 
	 * 获得课程模板信息
	 * 
	 * @param course 课堂模板
	 * @return 
	 */
	@RequestMapping(value="/course/template",method=RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<CourseInfo> getCourseTemplate(CourseInfo course){
		logger.info("【删除课堂模板接口】,参数:course=" + JSON.toJSONString(course));
		CourseInfo result = courseService.getCourseTemplate(course);
		return constructResponse(result);
	}
	
	
	/**
	 * 
	 * 获得课程模板信息
	 * 
	 * @param course 课堂模板
	 * @return 
	 */
	@RequestMapping(value="/course/maxnum",method=RequestMethod.GET)
	@ResponseBody
	public ResponseMessage<Integer> getCourseNum(CourseInfo course){
		logger.info("【查询大学一天的课的总数】,参数:course=" + JSON.toJSONString(course));
		Integer result = courseService.getCourseNum(course.getUniversityId());
		return constructResponse(result);
	}
	
	
	
	
	
	
}
